/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.order.service.serviceorder;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.order.dao.serviceorder.ServiceOrderDao;
import com.jeesite.modules.order.dao.serviceorder.ServiceOrderDetailDao;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务订单Service
 * @author Kilo
 * @version 2021-03-05
 */
@Service
@Transactional(readOnly=true)
public class ServiceOrderService extends CrudService<ServiceOrderDao, ServiceOrder> {

	@Autowired
	private ServiceOrderDetailDao serviceOrderDetailDao;
	@Autowired
	private ServiceOrderDao serviceOrderDao;

	/**
	 * 获取单条数据
	 * @param serviceOrder
	 * @return
	 */
	@Override
	public ServiceOrder get(ServiceOrder serviceOrder) {
		ServiceOrder entity = super.get(serviceOrder);
		if (entity != null){
			ServiceOrderDetail serviceOrderDetail = new ServiceOrderDetail(entity);
			serviceOrderDetail.setStatus(ServiceOrderDetail.STATUS_NORMAL);
			entity.setServiceOrderDetailList(serviceOrderDetailDao.findList(serviceOrderDetail));
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * @param serviceOrder 查询条件

	 * @return
	 */
	@Override
	public Page<ServiceOrder> findPage(ServiceOrder serviceOrder) {
		return super.findPage(serviceOrder);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param serviceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(ServiceOrder serviceOrder) {
		super.save(serviceOrder);
		// 保存上传附件
		FileUploadUtils.saveFileUpload(serviceOrder.getId(), "serviceOrder_file");
		// 保存 ServiceOrder子表
		for (int i = 0; i < serviceOrder.getServiceOrderDetailList().size(); i++) {
			ServiceOrderDetail serviceOrderDetail = serviceOrder.getServiceOrderDetailList().get(i);
			if (!ServiceOrderDetail.STATUS_DELETE.equals(serviceOrderDetail.getStatus())){
				serviceOrderDetail.setServiceOrderId(serviceOrder);
				serviceOrderDetail.setRowNumber((long) (i+1));
				if (serviceOrderDetail.getIsNewRecord()){
					serviceOrderDetailDao.insert(serviceOrderDetail);
				}else{
					serviceOrderDetailDao.update(serviceOrderDetail);
				}
			}else{
				serviceOrderDetailDao.delete(serviceOrderDetail);
			}
		}
	}

	/**
	 * 更新状态
	 * @param serviceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(ServiceOrder serviceOrder) {
		super.updateStatus(serviceOrder);
	}

	/**
	 * 删除数据
	 * @param serviceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(ServiceOrder serviceOrder) {
		super.delete(serviceOrder);
		ServiceOrderDetail serviceOrderDetail = new ServiceOrderDetail();
		serviceOrderDetail.setServiceOrderId(serviceOrder);
		serviceOrderDetailDao.deleteByEntity(serviceOrderDetail);
	}

    public List<ServiceOrderDetail> getFrameNumberByCondition(String investorId, String companyId) {
	    return serviceOrderDetailDao.getFrameNumberByCondition(investorId,companyId);
    }

    public ServiceOrderDetail getOrderDetailByFrameNumber(String frameNumber) {
	    return serviceOrderDetailDao.getOrderDetailByFrameNumber(frameNumber);
    }

    public Long updateInvestorCarContract(String investorCarContractId, String frameNumber) {
       return serviceOrderDetailDao.updateInvestorCarContract(investorCarContractId,frameNumber);
    }

    public ServiceOrderDetail getOrderDetailByFrame(String frameNumber) {
        return serviceOrderDetailDao.getOrderDetailByFrame(frameNumber);
    }
	public ServiceOrderDetail getDetail(ServiceOrderDetail serviceOrderDetail) {
		serviceOrderDetail =serviceOrderDetailDao.findList(serviceOrderDetail).get(0);
		return serviceOrderDetailDao.get(serviceOrderDetail);
	}
    public List<ServiceOrderDetail> findListDetail(ServiceOrderDetail serviceOrderDetail) {
	    return serviceOrderDetailDao.findList(serviceOrderDetail);
    }

    //找寻对应的服务详情
    public ServiceOrderDetail findByMany(String fram, String ser, String row) {
		ServiceOrderDetail serviceOrderDetail =new ServiceOrderDetail();
		if(ser!=null&&!"".equals(ser)){
			serviceOrderDetail.setFrameNumber(fram);
		}

		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(ser);
		serviceOrder =serviceOrderDao.get(serviceOrder);
		serviceOrderDetail.setServiceOrderId(serviceOrder);
		if(row!=null&&!"".equals(row)){
			serviceOrderDetail.setRowNumber(Long.parseLong(row));
		}
		serviceOrderDetail =serviceOrderDetailDao.findList(serviceOrderDetail).get(0);
		serviceOrderDetail =serviceOrderDetailDao.get(serviceOrderDetail);
		return  serviceOrderDetail;
    }

    public List<ServiceOrderDetail> findByServiceOrderId(String serviceOrderId){
		return serviceOrderDetailDao.findByServiceOrderId(serviceOrderId);
	}

	public List<ServiceOrderDetail> findAll(){
		return serviceOrderDetailDao.findAll();
	}

    public List<String> getFrameNumberByRemove(List<String> deleteFameNumbers) {
		return serviceOrderDetailDao.getFrameNumberByRemove(deleteFameNumbers);
    }
}
